package cn.tedu.ivosback.maintenance.service.impl;

import cn.tedu.ivosback.maintenance.mapper.MaintenanceMapper;
import cn.tedu.ivosback.maintenance.pojo.dto.MaintenanceParam;
import cn.tedu.ivosback.maintenance.pojo.dto.MaintenanceQuery;
import cn.tedu.ivosback.maintenance.pojo.entity.Maintenance;
import cn.tedu.ivosback.maintenance.pojo.vo.MaintenanceVO;
import cn.tedu.ivosback.maintenance.service.MaintenanceService;
import cn.tedu.ivosback.user.mapper.UserMapper;
import cn.tedu.ivosback.user.pojo.vo.UserVO;
import cn.tedu.ivosback.vehicle.mapper.VehicleMapper;
import cn.tedu.ivosback.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivosback.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MaintenanceServiceImpl implements MaintenanceService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Autowired
    MaintenanceMapper maintenanceMapper;

    @Override
    public List<MaintenanceVO> select(MaintenanceQuery maintenanceQuery) {
        log.debug("开始处理查询维修记录的Service层：{}", maintenanceQuery);
        if (maintenanceQuery.getLicense() != null && !maintenanceQuery.getLicense().isEmpty()) {
            maintenanceQuery.setVehicleId(vehicleMapper.selectByLicense(maintenanceQuery.getLicense()).getId());
        }
        List<MaintenanceVO> list = maintenanceMapper.select(maintenanceQuery);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setLicense(vehicleMapper.selectById(list.get(i).getVehicleId()).getLicense());
        }
        return list;
    }

    @Override
    public void save(MaintenanceParam maintenanceParam) {
        log.debug("开始处理添加维修记录的Service层：{}", maintenanceParam);
        Maintenance maintenance = new Maintenance();
        BeanUtils.copyProperties(maintenanceParam, maintenance);
        maintenance.setUserId(userMapper.selectByUsername(maintenanceParam.getUsername()).getId());
        maintenance.setVehicleId(vehicleMapper.selectByLicense(maintenanceParam.getLicense()).getId());
        if (maintenanceParam.getId() == null) {
            maintenance.setCreateTime(new Date());
            maintenanceMapper.insert(maintenance);
            Vehicle vehicle = new Vehicle();
            vehicle.setId(maintenance.getVehicleId());
            vehicle.setStatus("3");
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        } else {
            maintenance.setUpdateTime(new Date());
            maintenanceMapper.update(maintenance);
            if (maintenanceParam.getStatus() == "2") {
                Vehicle vehicle = new Vehicle();
                vehicle.setId(maintenance.getVehicleId());
                vehicle.setStatus("1");
                vehicle.setUpdateTime(new Date());
                vehicleMapper.update(vehicle);
            }
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理删除维修记录的Service层：{}", id);
        MaintenanceVO maintenanceVO = maintenanceMapper.selectById(id);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(maintenanceVO.getVehicleId());
        vehicle.setStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
        maintenanceMapper.delete(id);
    }
}
